//
//  DateDay.m
//  CalenderDemo
//
//  Created by GZMLUser on 14-11-6.
//  Copyright (c) 2014年 GZMLUser. All rights reserved.
//

#import "DateDay.h"

@implementation DateDay

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"DateDay" owner:self options:nil]lastObject];
    }
    return self;
}


-(void)setLightText:(BOOL)light
{
    if(light) {
        UIColor * color = [UIColor colorWithWhite:.84 alpha:1];
        [self.dateButton setTitleColor:color forState:UIControlStateNormal];
        self.dateButton.enabled = NO;
        
    }
    else {
        UIColor * color = [UIColor colorWithWhite:.3 alpha:1];
        [self.dateButton setTitleColor:color forState:UIControlStateNormal];
       
    }
}

#pragma mark -设置选中后的相关属性
-(void)setSelected:(BOOL)selected // 设置选中时候个属性
{
    if(selected) { // 选中日期
        [self setBackgroundColor:self.selectedBackgroundColor];
        [self.dateButton setSelected:YES];
        [self.dateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    else {
        [self setBackgroundColor:[UIColor whiteColor]];
        [self.dateButton setSelected:NO];
        [self.dateButton setTitleColor:[UIColor colorWithWhite:.3 alpha:1] forState:UIControlStateNormal];
    }
    
    if (self.currentDateColor && [self isToday]) { // 当前日期
        [self.dateButton setTitleColor:self.currentDateColor forState: UIControlStateNormal];
        // 可设置button的背景颜色
        [self.dateButton setBackgroundColor:[UIColor grayColor]];
        
        [self.dateButton.layer setCornerRadius:2.0f];
        [self.dateButton.layer setMasksToBounds:YES];
        
        [self.dateButton.layer setBorderWidth:.5f];
        [self.dateButton.layer setBorderColor:[UIColor grayColor].CGColor];
    }
}
- (BOOL)isToday
{
    NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self.date];
    NSDateComponents *today = [[NSCalendar currentCalendar] components:NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    return ([today day] == [otherDay day] &&
            [today month] == [otherDay month] &&
            [today year] == [otherDay year] &&
            [today era] == [otherDay era]);
}


- (IBAction)dateButtonTapped:(id)sender
{
    [self.delegate dateDayTapped:self];
}


@end
